package com.vmware.elastic.batch;

import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

/**
 * @apiNote 批量创建文档
 */
public class DocumentBatchCreate {
    public static void main(String[] args) throws Exception {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("10.192.193.98", 9200))
        );
        BulkRequest request = new BulkRequest("vmware");//bulk:大批的
        request.add(new IndexRequest().id("1005").source(XContentType.JSON,"name","wangwu","age",30));
        request.add(new IndexRequest().id("1006").source(XContentType.JSON,"name","wangwu2","age",40));
        request.add(new IndexRequest().id("1007").source(XContentType.JSON,"name","wangwu33","age",50));
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getTook());//6ms
        System.out.println(response.getItems());//[Lorg.elasticsearch.action.bulk.BulkItemResponse;@6bb4dd34
        client.close();
    }
}
